package com.hk.yajin.function.members.fragment;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.hk.developmentlib.baseben.NewWorkBean;
import com.hk.developmentlib.callback.HttpCallBack;
import com.hk.yajin.R;
import com.hk.yajin.base.BaseFragment;
import com.hk.yajin.function.members.activity.AcountDistrictListActivity;
import com.hk.yajin.function.members.activity.RegisterSmsCodeActivity;
import com.hk.yajin.utills.HttpRequestUtils;
import com.hk.yajin.utills.url.UrlConstants;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 伦哥哥 on 2017/7/10.
 */

public class RegisterFragment extends BaseFragment {
    @BindView(R.id.tv_select_district)
    TextView tvSelectDistrict;
    @BindView(R.id.iv_select_district)
    ImageView ivSelectDistrict;
    @BindView(R.id.tv_district_num)
    TextView tvDistrictNum;
    @BindView(R.id.et_acount)
    EditText etAcount;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.tv_registe)
    TextView tv_Registe;


    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container) {
        mContentView = inflater.inflate(R.layout.fragment_register, container, false);
        ButterKnife.bind(this, mContentView);
        return mContentView;
    }

    @OnClick({R.id.tv_select_district, R.id.iv_select_district, R.id.tv_district_num, R.id.tv_registe})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_select_district:
            case R.id.iv_select_district:
            case R.id.tv_district_num:
                startActivityForResult(new Intent(getContext(), AcountDistrictListActivity.class), Activity.RESULT_FIRST_USER);
                break;
            case R.id.tv_registe:
                requestSmsCode();
                break;
        }
    }


    private void requestSmsCode() {
        String acount = etAcount.getText().toString();
        String pwd = etPwd.getText().toString();
        if (TextUtils.isEmpty(acount)) {
            showShortToast("账号不能为空");
            return;
        }
        if (TextUtils.isEmpty(pwd)) {
            showShortToast("密码不能为空");
            return;
        }



//        if (acount.length() < 11 || !acount.startsWith("1")) {
//            showShortToast("请输入有效的手机号");
//            return;
//        }

        if (pwd.length() < 6) {
            showShortToast("密码不能低于六位");
            return;
        }

        if (isNetWorkConnect()) {
            showLoadingView();
            Map<String, Object> reqParam = new HashMap<String, Object>();
            reqParam.put("mobile", acount);
            reqParam.put("type", "register");
            reqParam.put("mobile_area_code", tvDistrictNum.getText().toString());
            HttpRequestUtils.post(false, UrlConstants.FUNCTION_GET_SMS_CODE, reqParam, false, new HttpCallBack() {

                @Override
                public void onSucced(String data) {
                    hideLoadingView();
                    Intent registerIntent = new Intent(getActivity(), RegisterSmsCodeActivity.class);
                    registerIntent.putExtra(RegisterSmsCodeActivity.ACOUNT, etAcount.getText().toString());
                    registerIntent.putExtra(RegisterSmsCodeActivity.PASSWORD, etPwd.getText().toString());
                    startActivity(registerIntent);
                }

                @Override
                public void onErrorCode(NewWorkBean bean) {
                    hideLoadingView();
                    showShortToast(bean.getMsg());
                }

                @Override
                public void onBadHttp(String tip) {
                    hideLoadingView();
                    showShortToast(tip);
                }

            });
        }

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == Activity.RESULT_FIRST_USER && resultCode == Activity.RESULT_OK && data != null) {
            tvDistrictNum.setText(data.getStringExtra(AcountDistrictListActivity.DISTRICT_NUM));
            tvSelectDistrict.setText(data.getStringExtra(AcountDistrictListActivity.DISTRICT_NAME));
        }
    }
}
